package com.micmac.customer.view.mediator
{
	import com.micmac.customer.events.CustomerEvent;
	import com.micmac.customer.interfaces.ICustomerService;
	import com.micmac.customer.model.PricingModel;
	import com.micmac.customer.view.PricingView;
	import com.micmac.customer.vo.PricingModelVO;
	import com.micmac.customer.vo.PricingTypeVO;
	
	import flash.events.Event;
	
	import org.robotlegs.mvcs.Mediator;
	import org.robotlegs.utilities.modular.mvcs.ModuleMediator;
	
	public class PricingViewMediator extends ModuleMediator
	{
		[Inject]
		public var pricingView:PricingView
		[Inject]
		public var customerService:ICustomerService;
		[Inject]
		public var dataModel:PricingModel;
		
		private var _pricingModelIndex:int = 0;
		private var _pricingTypeIndex:int = 0;
		
		override public function onRegister():void
		{
			addViewListener(PricingView.SAVE_PRICING_MODEL, onSavePricingModel);
			addViewListener(PricingView.SAVE_PRICING_TYPE,  onSavePricingType);
			
			addViewListener(PricingView.STACK_CHANGED, onStackChanged);
			
			eventMap.mapListener(eventDispatcher,CustomerEvent.PRICING_MODEL_SAVED,onPricingModelSaved);
			eventMap.mapListener(eventDispatcher,CustomerEvent.PRICING_TYPE_SAVED,onPricingTypeSaved);
			
			addViewListener(PricingView.NEXT_PRICING_MODEL,onNextPricingModel);
			addViewListener(PricingView.PREVIOUS_PRICING_MODEL,onPreviousPricingModel);
			addViewListener(PricingView.NEXT_PRICING_TYPE,onNextPricingType);
			addViewListener(PricingView.PREVIOUS_PRICING_TYPE,onPrevousPricingType);
			
			/*addViewListener(UserView.PREVIOUS_USER_TITLE,onPreviousUserTitle);
			addViewListener(UserView.NEXT_USER_TITLE,onNextUserTitle);
			
			addViewListener(UserView.SAVE_USER,onSaveUser);
			addViewListener(UserView.PREVIOUS_USER,onPreviousUser);
			addViewListener(UserView.NEXT_USER,onNextUser);
			
			addViewListener(UserView.SAVE_ROLE,onSaveUserRole);
			addViewListener(UserView.PREVIOUS_USER_ROLE,onPreviousUserRole);
		
			
			addViewListener(UserView.UPLOAD_CLICKED,onBrowse);
			
			
			
			
			eventMap.mapListener(eventDispatcher,CustomerEvent.USER_ROLE_SAVED,onUserRoleSaved);*/
		}
		
		private function onPrevousPricingType(event:Event):void
		{
			
			_pricingTypeIndex--;
			pricingView.btnNext.enabled = true;
			pricingView.btnSave.enabled = false;
			
			pricingView.txtPricingTypelName.text = dataModel.pricingTypeList[_pricingTypeIndex].pricingTypeName;
			pricingView.txtPricingTypeDscr.text = dataModel.pricingTypeList[_pricingTypeIndex].description;
			pricingView.cmbOrg.textInput.text = dataModel.pricingTypeList[_pricingTypeIndex].org;
			pricingView.cmbPricingTypeStatus.textInput.text = dataModel.pricingTypeList[_pricingTypeIndex].status;
			
			if(_pricingTypeIndex == 0)
			{
				pricingView.btnPrev.enabled = false;
			}
			
		}
		private function onNextPricingType(event:Event):void
		{
			_pricingTypeIndex++;
			pricingView.btnPrev.enabled = true;
			pricingView.btnSave.enabled = false;
			
			
			pricingView.txtPricingTypelName.text = dataModel.pricingTypeList[_pricingTypeIndex].pricingTypeName;
			pricingView.txtPricingTypeDscr.text = dataModel.pricingTypeList[_pricingTypeIndex].description;
			pricingView.cmbOrg.textInput.text = dataModel.pricingTypeList[_pricingTypeIndex].org;
			pricingView.cmbPricingTypeStatus.textInput.text = dataModel.pricingTypeList[_pricingTypeIndex].status;
			
			
			if(_pricingTypeIndex == dataModel.pricingTypeList.length - 1)
			{
				pricingView.btnNext.enabled = false;
			}
		}
			
		private function onPreviousPricingModel(event:Event):void
		{
			
			_pricingModelIndex--;
			pricingView.btnNext.enabled = true;
			pricingView.btnSave.enabled = false;
			pricingView.txtPricingModelName.text = dataModel.pricingModelList[_pricingModelIndex].pricingModelName;
			pricingView.txtPricingModelDscr.text = dataModel.pricingModelList[_pricingModelIndex].description;
			pricingView.txtPackBase.text = dataModel.pricingModelList[_pricingModelIndex].packBase;
			pricingView.txtPackIncr.text = dataModel.pricingModelList[_pricingModelIndex].packIncr;
			pricingView.cmbPricingModelStatus.textInput.text = dataModel.pricingModelList[_pricingModelIndex].status;
			
			if(_pricingModelIndex == 0)
			{
				pricingView.btnPrev.enabled = false;
			}
			
		}
		private function onNextPricingModel(event:Event):void
		{
			_pricingModelIndex++;
			pricingView.btnPrev.enabled = true;
			pricingView.btnSave.enabled = false;
			
			
			pricingView.txtPricingModelName.text = dataModel.pricingModelList[_pricingModelIndex].pricingModelName;
			pricingView.txtPricingModelDscr.text = dataModel.pricingModelList[_pricingModelIndex].description;
			pricingView.txtPackBase.text = dataModel.pricingModelList[_pricingModelIndex].packBase;
			pricingView.txtPackIncr.text = dataModel.pricingModelList[_pricingModelIndex].packIncr;
			pricingView.cmbPricingModelStatus.textInput.text = dataModel.pricingModelList[_pricingModelIndex].status;
			
			
			if(_pricingModelIndex == dataModel.pricingModelList.length - 1)
			{
				pricingView.btnNext.enabled = false;
			}
		}
		private function onPricingModelSaved(event:Event = null):void
		{
			if(dataModel.pricingModelList.length != 1 && dataModel.pricingModelList.length != 0)
			{
				pricingView.btnPrev.enabled = true;
			}else
			{
				pricingView.btnPrev.enabled = false;
			}
			pricingView.btnAdd.enabled = true;
			pricingView.btnSave.enabled = false;
			pricingView.btnNext.enabled = false;
			_pricingModelIndex = dataModel.pricingModelList.length - 1;
			//pricingView.btnAdd.label = '+model';
		}
		private function onPricingTypeSaved(event:Event = null):void
		{
			if(dataModel.pricingTypeList.length != 1 && dataModel.pricingTypeList.length != 0)
			{
				pricingView.btnPrev.enabled = true;
			}else
			{
				pricingView.btnPrev.enabled = false;
			}
			pricingView.btnAdd.enabled = true;
			pricingView.btnSave.enabled = false;
			pricingView.btnNext.enabled = false;
			_pricingTypeIndex = dataModel.pricingTypeList.length - 1;
			//userView.addTitleBtn.label = '+user';
		}
		private function onStackChanged(event:Event):void
		{
			switch(pricingView.vsPricing.selectedIndex)
			{
				case 0:
				{
					onPricingModelSaved();
					pricingView.btnAdd.label = '+model';
					//pricingView.btnSave.enabled = true;
					//pricingView.btnAdd.enabled = true;
					break;
				}
				case 1:
				{
					onPricingTypeSaved();
					pricingView.btnAdd.label = '+type';
					//pricingView.btnSave.enabled = true;
					//pricingView.btnAdd.enabled = true;
					break;
				}
				default:
				{
					break;
				}
			}
		}
		private function onSavePricingModel(event:Event):void
		{
			var pricingModelVO:PricingModelVO = new PricingModelVO();
			pricingModelVO.pricingModelName = pricingView.txtPricingModelName.text;
			pricingModelVO.description = pricingView.txtPricingModelDscr.text;
			pricingModelVO.packBase = pricingView.txtPackBase.text;
			pricingModelVO.packIncr = pricingView.txtPackIncr.text;
			pricingModelVO.status = pricingView.cmbPricingModelStatus.textInput.text;
			customerService.savePricingModel(pricingModelVO);
		}
		private function onSavePricingType(event:Event):void
		{
			var pricingTypeVO:PricingTypeVO = new PricingTypeVO();
			pricingTypeVO.pricingTypeName = pricingView.txtPricingTypelName.text;
			pricingTypeVO.description = pricingView.txtPricingTypeDscr.text;
			pricingTypeVO.org = pricingView.cmbOrg.textInput.text;
			pricingTypeVO.status = pricingView.cmbPricingTypeStatus.textInput.text;
			customerService.savePricingType(pricingTypeVO);
		}
	}
}